package com.etoak.common.web.context;

import com.etoak.common.jwt.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

public class LoginContext {
    public static String getUserId(){
        HttpServletRequest request = getCurrentRequest();
        String jwt = request.getHeader(HttpHeaders.AUTHORIZATION);
        Map<String,Object> claimsMAp = JwtUtil.parse(jwt);
        return claimsMAp.get("id").toString();
    }

    /**
     * 获取当前 HttpServletRequest 对象
     * @return
     */
    public static HttpServletRequest getCurrentRequest(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }
}
